package me.firesun.wechat.enhancement.plugin;

import android.content.ContentValues;
import android.content.Context;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import me.firesun.wechat.enhancement.PreferencesUtils;
import me.firesun.wechat.enhancement.util.HookParams;

public class AutoReply implements IPlugin {
    private static Map<Long, Object> msgCacheMap = new HashMap<>();
    private static Object storageInsertClazz;



    @Override
    public void hook(final XC_LoadPackage.LoadPackageParam lpparam) {

        XposedHelpers.findAndHookMethod(HookParams.getInstance().SQLiteDatabaseClassName, lpparam.classLoader, HookParams.getInstance().SQLiteDatabaseInsertMethod, String.class, String.class, ContentValues.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                String tableName = (String) param.args[0];
                ContentValues contentValues = (ContentValues) param.args[2];
                if (tableName == null || tableName.length() == 0 || contentValues == null) {
                    return;
                }
                //过滤掉非聊天消息
                if (!tableName.equals("message")) {
                    return;
                }
                //打印出日志
//                        printInsertLog(tableName, (String) param.args[1], contentValues, (Integer) param.args[3]);

                //提取消息内容
                //1：表示是自己发送的消息
                int isSend = contentValues.getAsInteger("isSend");
                //消息内容
                String strContent = contentValues.getAsString("content");
                //说话人ID
                String strTalker = contentValues.getAsString("talker");

                //收到消息，进行回复（要判断不是自己发送的、不是群消息、不是公众号消息，才回复）
                Log.i("mymsg",contentValues.toString());
                if (isSend != 1 && !strTalker.endsWith("@chatroom") && !strTalker.startsWith("gh_")){
                    handlemsg(lpparam,strTalker,strContent);
                }


            }
        });


//        XposedHelpers.findAndHookMethod("com.tencent.mm.ui.chatting.c.aj", lpparam.classLoader, "aFy", String.class, new XC_MethodHook() {
//            @Override
//            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                param.args[0]="你真帅";
//            }
//        });

//        XposedHelpers.findAndHookMethod(
//                "com.tencent.mm.f.c.dm",
//                lpparam.classLoader,
//                "mj",
//                String.class,
//                new XC_MethodHook() {
//                    @Override
//                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                        String vid=(String)param.args[0];
//                        Log.i("mymsg", "拦截mj str:"+vid);
//                        Log.i("mymsg", "堆栈打印\n"+Log.getStackTraceString(new Throwable()));
//                    }
//                }
//        );






    }//hook

    private  void handlemsg(final XC_LoadPackage.LoadPackageParam lpparam,String strTalker,String strContent){
        SystemClock.sleep(1000);
        Class<?> clz_h=XposedHelpers.findClass("com.tencent.mm.modelmulti.h",lpparam.classLoader);
        Object message = XposedHelpers.newInstance(clz_h, strTalker, strContent, 1,1);
        Class<?> clz_aw=XposedHelpers.findClass("com.tencent.mm.model.az",lpparam.classLoader);
        Object o=XposedHelpers.callStaticMethod(clz_aw,"aez");
        XposedHelpers.callMethod(o,"a",message,0);
    }
}
